package com.chat.chat.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chat.chat.beans.ChatVo;
import com.chat.chat.beans.kefu;
import com.chat.chat.service.ChatService;
import com.chat.chat.service.KefuService;
import com.chat.chat.untils.R;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.stream.Collectors;


/**
 * (Kefu)表控制层
 *
 * @author makejava
 * @since 2024-03-04 10:11:17
 */
@RestController
@RequestMapping("kefu")
public class KefuController  {

    @Resource
    private ChatService chatService;

    @Resource
    private KefuService kefuService;

    //分配客服
    @GetMapping("chavoByid")
    public List<ChatVo> chavoByid(String id) {
        LambdaQueryWrapper<ChatVo> queryWrapper = new LambdaQueryWrapper<>();
        List<ChatVo> list = chatService.list(queryWrapper.eq(ChatVo::getId, id));
        queryWrapper.clear();
        List<ChatVo> list2 = chatService.list(queryWrapper.eq(ChatVo::getTo, id));
        list.addAll(list2);
        List<ChatVo> collect = list.stream().sorted(Comparator.comparing(ChatVo::getIdd)).collect(Collectors.toList());
        return collect;
    }

    //分配客服
    @GetMapping("getKefu")
    public kefu getu() {
        List<com.chat.chat.beans.kefu> list = kefuService.list(new LambdaQueryWrapper<kefu>().eq(com.chat.chat.beans.kefu::getStatus, 1).orderByDesc(kefu::getJieDai));
       if(list.isEmpty()){
           return null;
       }
        kefu kefu = list.get(0);
        int dai = Integer.parseInt(kefu.getJieDai())+1;
        kefu.setJieDai(String.valueOf(dai));
        kefuService.updateById(kefu);
        if (!list.isEmpty()) {
            return kefu;
        }
        return null;
    }

    //问题回答
    @GetMapping("question")
    public ChatVo question(String question) {
        LambdaQueryWrapper<ChatVo> chatVoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        ChatVo one = chatService.getOne(chatVoLambdaQueryWrapper.like(ChatVo::getName, question).eq(ChatVo::getType, 2));
        if (one==null){
            one = new ChatVo();
            one.setType("2").setMsg("请稍等，为您转接人工客服~");
        }
        return one;
    }

    @PostMapping("insertMsg")
    public boolean insertMsg(@RequestBody ChatVo chatVo) {
        return chatService.save(chatVo);
    }

    @GetMapping("xiuxi")
    public boolean xiuxi(String id,String status) {
        kefu kefu = new kefu();
        kefu.setId(Integer.valueOf(id));
        kefu.setStatus(Integer.valueOf(status));
        boolean b = kefuService.updateById(kefu);
        return b;
    }

    @GetMapping("getIdex")
    public R<?> getIdex(String id) {
        LambdaQueryWrapper<kefu> queryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<ChatVo> queryWrapper1 = new LambdaQueryWrapper<>();
        kefu one = kefuService.getOne(queryWrapper.eq(kefu::getId, id));
        int count = chatService.count(queryWrapper1.eq(ChatVo::getId, id));
        LocalDate startDate = LocalDate.of(2024,3 , 14);

        // 结束日期：当前日期
        LocalDate endDate = LocalDate.now();

        // 计算总天数
        long daysBetween = ChronoUnit.DAYS.between(startDate, endDate);

        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("pingjunTime","6.5h");
        stringObjectHashMap.put("workTime",daysBetween);
        stringObjectHashMap.put("jiedai",one.getJieDai());
        stringObjectHashMap.put("huihua",count);



        return R.ok(stringObjectHashMap);

    }

}

